<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="public/css.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <title>tranthequang.it</title>
    </head>
    <body>
        <h1>Tran The Quang</h1>
        <?php
        require_once 'model/db.php';

        $query = "SELECT * 
      FROM pages 
      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
      INNER JOIN apps ON apps.id=apps_pages.id_app
      INNER JOIN companies ON companies.id=apps.id_company
      WHERE (apps.id_company = Company) AND (apps.id = App)";
        $query1 = "SELECT * FROM pages ";
        $pages = getPages($query);
        $query = "SELECT * FROM companies ";
        $companies = getPages($query);
        $query = "SELECT * FROM apps ";
        $apps = getPages($query);
        $keyword = NULL;
        $ratingMin = NULL;
        $ratingMax = NULL;
        $Company = NULL;
        $App = NULL;
        ?>

        <form method="POST">
            Keyword  : <input type="text" name="keyword"><br>
            Company  :  <select name="Company">
                <option value="0"><?php echo "<-------Company------->" ?></option>
                <?php foreach ($companies as $company): ?>
                    <option value="<?php echo $company['id'] ?>"><?php echo $company["name"] ?></option>
                <?php endforeach; ?>
            </select> <br>
            App  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:  <select name="App">
                <option value="0"><?php echo "<-------App------->" ?></option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['pid'] ?>"><?php echo $app["name"] ?></option>
                <?php endforeach; ?>
            </select> <br>
            Min fans &nbsp;: <input type="text" name="ratingMin"><br>
            Max fans : <input type="text" name="ratingMax"><br>
            <input type="submit" name="submit" value="Search">
        </form>
        <?php if (isset($_POST['submit'])): ?>
            <?php $keyword = $_POST['keyword']; ?>
            <?php $ratingMin = (double) $_POST['ratingMin']; ?>
            <?php $ratingMax = (double) $_POST['ratingMax']; ?>
            <?php $Company = $_POST['Company']; ?>
            <?php $App = $_POST['App']; ?>
        <?php endif; ?>

        <table id="table1" border="1px">
            <tr class="khung">
                <td class="pid">STT </td>
                <td class="id_page">id_page </td>
                <td class="name">name </td>
                <td class="company">company </td>
                <td class="likes">likes </td>
                <td class="talking">talking</td>
                <td class="category">category </td>
                <td class="link">link </td>
                <td class="website">website </td>
                <td class="picture">picture </td>
                <td class="description">description </td>
            </tr>
            <?php foreach ($pages as $page): ?>
                <?php if ($Company == 0): ?>
                    <?php if (empty($keyword)): ?>
                        <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["company"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php elseif (empty($ratingMax)): ?>
                            <?php if ($ratingMin <= (double) $page["likes"]): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php endif; ?>
                        <?php else: ?>
                            <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php endif; ?>
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if (strstr($page["description"], $keyword)): ?>
                            <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php elseif (empty($ratingMax)): ?>
                                <?php if ($ratingMin <= (double) $page["likes"]): ?>
                                    <tr>
                                        <td><?php echo $page["pid"] ?></td>
                                        <td><?php echo $page["id"] ?></td>
                                        <td><?php echo $page["name"] ?></td>
                                        <td><?php echo $page["likes"] ?></td>
                                        <td><?php echo $page["talking_about_count"] ?></td>
                                        <td><?php echo $page["category"] ?></td>
                                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                                    </tr>
                                <?php endif; ?>
                            <?php else: ?>
                                <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                                    <tr>
                                        <td><?php echo $page["pid"] ?></td>
                                        <td><?php echo $page["id"] ?></td>
                                        <td><?php echo $page["name"] ?></td>
                                        <td><?php echo $page["likes"] ?></td>
                                        <td><?php echo $page["talking_about_count"] ?></td>
                                        <td><?php echo $page["category"] ?></td>
                                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                                    </tr>
                                <?php endif; ?>
                            <?php endif; ?>
                        <?php endif; ?> 
                    <?php endif; ?> 


                <?php else: ?>


                    <?php if ($Company == $company['id']): ?>
                        <?php if (empty($keyword)): ?>
                            <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php elseif (empty($ratingMax)): ?>
                                <?php if ($ratingMin <= (double) $page["likes"]): ?>
                                    <tr>
                                        <td><?php echo $page["pid"] ?></td>
                                        <td><?php echo $page["id"] ?></td>
                                        <td><?php echo $page["name"] ?></td>
                                        <td><?php echo $page["likes"] ?></td>
                                        <td><?php echo $page["talking_about_count"] ?></td>
                                        <td><?php echo $page["category"] ?></td>
                                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                                    </tr>
                                <?php endif; ?>
                            <?php else: ?>
                                <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                                    <tr>
                                        <td><?php echo $page["pid"] ?></td>
                                        <td><?php echo $page["id"] ?></td>
                                        <td><?php echo $page["name"] ?></td>
                                        <td><?php echo $page["likes"] ?></td>
                                        <td><?php echo $page["talking_about_count"] ?></td>
                                        <td><?php echo $page["category"] ?></td>
                                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                                    </tr>
                                <?php endif; ?>
                            <?php endif; ?>
                        <?php else: ?>
                            <?php if (strstr($page["description"], $keyword)): ?>
                                <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                                    <tr>
                                        <td><?php echo $page["pid"] ?></td>
                                        <td><?php echo $page["id"] ?></td>
                                        <td><?php echo $page["name"] ?></td>
                                        <td><?php echo $page["likes"] ?></td>
                                        <td><?php echo $page["talking_about_count"] ?></td>
                                        <td><?php echo $page["category"] ?></td>
                                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                                    </tr>
                                <?php elseif (empty($ratingMax)): ?>
                                    <?php if ($ratingMin <= (double) $page["likes"]): ?>
                                        <tr>
                                            <td><?php echo $page["pid"] ?></td>
                                            <td><?php echo $page["id"] ?></td>
                                            <td><?php echo $page["name"] ?></td>
                                            <td><?php echo $page["likes"] ?></td>
                                            <td><?php echo $page["talking_about_count"] ?></td>
                                            <td><?php echo $page["category"] ?></td>
                                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                                        </tr>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                                        <tr>
                                            <td><?php echo $page["pid"] ?></td>
                                            <td><?php echo $page["id"] ?></td>
                                            <td><?php echo $page["name"] ?></td>
                                            <td><?php echo $page["likes"] ?></td>
                                            <td><?php echo $page["talking_about_count"] ?></td>
                                            <td><?php echo $page["category"] ?></td>
                                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                                        </tr>
                                    <?php endif; ?>
                                <?php endif; ?>
                            <?php endif; ?> 
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endforeach; ?>
        </table>
        <script type="text/javascript">
            var txtmin = document.getElementById('min_fans').value;
            var txtmax = document.getElementById('max_fans').value;
            alert("txtmin is: " + txtmin + "txtmax is: " + txtmax);
        </script>
        <script type="text/javascript">
            $('min_fans').click(function() {
                alert('$min_fans').value();
            })
        </script>    
    </body>
</html>